﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace BrnMall.WeiXin.Sdk.Domain
{

    /// <summary>
    /// 模板消息
    /// </summary>
    public class TemplateMessage
    {
        /// <summary>
        /// 接收者（用户）的 openid
        /// </summary>
        public string touser { get; set; }
        /// <summary>
        /// 所需下发的模板消息的id
        /// </summary>
        public string template_id { get; set; }
        /// <summary>
        /// 表单提交场景下，为 submit 事件带上的 formId；支付场景下，为本次支付的 prepay_id
        /// </summary>
        public string form_id { get; set; }
        /// <summary>
        /// 模板内容，不填则下发空模板
        /// </summary>
        public object data { get; set; }
        /// <summary>
        /// 网页链接
        /// </summary>
        public string url { get; set; } = string.Empty;
        /// <summary>
        /// 小程序链接
        /// </summary>
        public TemplateMiniProgram miniprogram { get; set; }
        /// <summary>
        /// 小程序链接(已过时)
        /// </summary>
        public string page { get; set; } = string.Empty;
    }

    /// <summary>
    /// 订阅消息
    /// </summary>
    public class SubscribeMessage
    {
        /// <summary>
        /// 接收者（用户）的 openid
        /// </summary>
        public string touser { get; set; }
        /// <summary>
        /// 所需下发的模板消息的id
        /// </summary>
        public string template_id { get; set; }
        /// <summary>
        /// 订阅消息
        /// </summary>
        public Dictionary<string, SubDataValue> data { get; set; }
        /// <summary>
        /// 点击模板卡片后的跳转页面，仅限本小程序内的页面。支持带参数,（示例index?foo=bar）。该字段不填则模板无跳转。
        /// </summary>
        public string page { get; set; } = "";
    }
    /// <summary>
    /// 小程序链接
    /// </summary>
    public class TemplateMiniProgram
    {
        /// <summary>
        /// 小程序appid
        /// </summary>
        public string appid { get; set; } = string.Empty;
        /// <summary>
        /// 小程序路径，支持带参数(示例index?foo=bar)
        /// </summary>
        public string pagepath { get; set; }
    }
    /// <summary>
    /// 模板内容
    /// </summary>
    public class TemplateData
    {
        public TemplateKeyWord first { get; set; }
        public TemplateKeyWord keyword1 { get; set; }
        public TemplateKeyWord keyword2 { get; set; }
        public TemplateKeyWord keyword3 { get; set; }
        public TemplateKeyWord keyword4 { get; set; }
        public TemplateKeyWord keyword5 { get; set; }
        public TemplateKeyWord keyword6 { get; set; }
        public TemplateKeyWord keyword7 { get; set; }

        public TemplateKeyWord remark { get; set; }

        /// <summary>
        /// 模板id
        /// </summary>
        public string templateId { get; set; }

        public string PageUrl { get; set; } = string.Empty;

        /// <summary>
        /// 关键词表达式
        /// </summary>
        public string Rule { get; set; }
        public Dictionary<string, SubDataValue> ToData()
        {
            var dic = new Dictionary<string, SubDataValue>();
            Regex r = new Regex("{{([\\s\\S].*?)}}");
            var matches = r.Matches(Rule);
            foreach (Match match in matches)
            {
                TemplateKeyWord value = null;
                switch (dic.Count)
                {
                    case 0: value = keyword1; break;
                    case 1: value = keyword2; break;
                    case 2: value = keyword3; break;
                    case 3: value = keyword4; break;
                    case 4: value = keyword5; break;
                    case 5: value = keyword6; break;
                    case 6: value = keyword7; break;
                    default: value = new TemplateKeyWord(); break;
                }
                dic.Add(match.Groups[1].Value, value);
            }
            return dic;
        }

        public Dictionary<string, SubDataValue> ToAlipayData()
        {
            var dic = new Dictionary<string, SubDataValue>();
            if (keyword1 != null)
                dic.Add("keyword1", keyword1);
            if (keyword2 != null)
                dic.Add("keyword2", keyword2);
            if (keyword3 != null)
                dic.Add("keyword3", keyword3);
            if (keyword4 != null)
                dic.Add("keyword4", keyword4);
            if (keyword5 != null)
                dic.Add("keyword5", keyword5);
            if (keyword6 != null)
                dic.Add("keyword6", keyword6);
            return dic;
        }
    }

    /// <summary>
    /// 模板内容value
    /// </summary>
    public class TemplateKeyWord : SubDataValue
    {
        //public string value { get; set; }
        public string color { get; set; }

        public TemplateKeyWord()
        {
            value = string.Empty;
            color = "#173177";
        }
        public TemplateKeyWord(string v, string c = "#173177")
        {
            value = v;
            color = c;
        }
    }

    /// <summary>
    /// 订阅消息Value
    /// </summary>
    public class SubDataValue
    {
        public string value { get; set; }
        public SubDataValue() { }
        public SubDataValue(string v)
        {
            this.value = v;
        }
    }

    /// <summary>
    /// 模板关键词详情
    /// </summary>
    public class KeyWordDetail
    {
        /// <summary>
        /// 关键词 id，选用模板时需要
        /// </summary>
        public int kid { get; set; }
        /// <summary>
        /// 关键词内容
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 内容对应的示例
        /// </summary>
        public string example { get; set; }
        /// <summary>
        /// 参数类型
        /// </summary>
        public string rule { get; set; }

    }
}
